﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PPI.Client.Logic.Inspection;
using PPI.Client.Utils;
using PPI.Entities.Enumerator;
using PPI.Entities.Inspections;
using PPI.Entities.XML;
using PPI.General;

namespace PPI.Client.Presenter
{
    public interface IInspectionCRUD
    {
        EXmlOutboxBase Database { get;}

    }

    public class InspectionCRUDPresenter
    {
        private IInspectionCRUD m_view;

        public InspectionCRUDPresenter(IInspectionCRUD view)
        {
            m_view = view;
        }

        public void SaveInspectionResult(EInspection inspection,List<EInspectionImage> imagesToDelete, string connectionString )
        {

            try
            {
                switch (m_view.Database.DatabaseType)
                {
                    case DatabaseTypes.Inspection:
                        SaveMobile(imagesToDelete, connectionString, inspection);
                        break;
                    case DatabaseTypes.Project:
                        SaveProject(imagesToDelete, inspection);
                        DeleteMobile(inspection, connectionString);
                        SaveMobileProject(imagesToDelete, connectionString, inspection);
                        break;
                }
            }
            catch (Exception ex)
            {
                RegisterError.SaveError(ex);
            }
           
        }

        private void SaveProject(List<EInspectionImage> imagesToDelete, EInspection inspection)
        {
            foreach (EInspectionImage image in imagesToDelete)
            {
                LInspectionImage.Delete(image);
            }
            LInspection.Save(inspection);
            LInspectionResult.Save(inspection.Result);

            WebUploaderHelper _UploaderHelper = new WebUploaderHelper(Constants.K_WEBSITEUPLOADER);
            foreach (EInspectionImage i in inspection.Images)
            {
                bool isToUpload = false;
                if (i.Id == Int32.MinValue)
                    isToUpload = true;
                LInspectionImage.Save(i);
                if (isToUpload)
                {
                    string _FileName = i.Inspection.Id + "_" + i.Id + ".jpg";
                    string _FolderName = DirHelper.GetImageDirectory(i.Inspection.Id, i.Id);

                    if (i.ImageFile.Exists)
                        _UploaderHelper.Upload(_FolderName, _FileName, i.ImageFile, Enum.GetName(typeof(DatabaseTypes), DatabaseTypes.Inspection));

                }

            }

            //foreach (EInspectionImage i in inspectionResult.Inspection.Images)
            //{
            //    LInspectionImage.Save(i);
            //}
        }

        private void SaveMobile(List<EInspectionImage> imagesToDelete, string connectionString, EInspection inspection)
        {
            foreach (EInspectionImage image in imagesToDelete)
            {
                LInspectionImage.DeleteMobile(image,connectionString);
            }
            LInspection.SaveMobile(inspection, connectionString);
            LInspectionResult.SaveMobile(inspection.Result,connectionString);
        }

        private void SaveMobileProject(List<EInspectionImage> imagesToDelete, string connectionString, EInspection inspection)
        {
            LInspection.InsertMobileProject(inspection,connectionString);
            LInspectionResult.InsertMobileProject(inspection.Result, connectionString);

            foreach (EInspectionImage i in inspection.Images)
            {
                LInspectionImage.InsertMobileProject(i, connectionString);
            }

            foreach (EInspectionImage image in imagesToDelete)
            {
                LInspectionImage.DeleteMobile(image, connectionString);
            }
        }

        private void DeleteMobile(EInspection inspection, string connectionString)
        {
            foreach (EInspectionImage i in inspection.Images)
            {
                LInspectionImage.DeleteMobile(i, connectionString);
            }
            LInspection.DeleteMobile(inspection, connectionString);
            LInspectionResult.DeleteMobile(inspection.Result, connectionString);
        }
    }
}
